//财务管理-定价
App.util.ns('Admin.Finance.Price.Form');
Admin.Finance.Price.Form.Timeprice = function(config){
    this.config = config;
    this.name = this.config.name;
    this.form = null;
    this.frame = null;
    this.items = [];
    this.mainItem = null;
    //初始化
    this.init();
};
Admin.Finance.Price.Form.Timeprice.prototype = {
    constructor: Admin.Finance.Price.Form.Timeprice,
    init: function(){
        var self = this;
        this.frame = $([
            '<div class="program-epg-container">',
            '   <div class="item main">',
            '       <div class="input-group" style="width:100%">',
            '           <input type="text" class="form-control begin-time" placeholder="开始时间">',
            '           <span class="input-group-addon" style="border-left: none;border-right: none;">-</span>',
            '           <input type="text" class="form-control end-time" placeholder="结束时间">',
            '           <span class="input-group-addon" style="border-left: none;border-right: none;">></span>',
            '           <input type="text" class="form-control unit" placeholder="计费单位">',
            '           <span class="input-group-addon" style="border-left: none;border-right: none;">:</span>',
            '           <input type="text" class="form-control price" placeholder="单位价格">',
            '           <span class="input-group-btn">',
            '               <button class="btn btn-default btn-sm btn-add" type="button" style="border-radius: 0;"><i class="fa fa-plus"></i></button>',
            '           </span>',
            '       </div>',
            '       <span class="form-static" style="display: none;"></span>',
            '   </div>',
            '</div>'
        ].join(''));
        this.frame.find('.begin-time').css({float: 'left', width: '90px', height: '30px'});
        this.frame.find('.input-group-addon').css({float: 'left', width: '30px', height: '30px'});
        this.frame.find('.end-time').css({float: 'left', width: '90px', height: '30px'});
        this.frame.find('.unit').css({float: 'left', width: '90px', height: '30px'});
        this.frame.find('.price').css({float: 'left', width: 'calc(100% - 390px)', height: '30px'});
        this.frame.find('.input-group-btn').css({float: 'left'});
        this.frame.find('.btn-add').click(function(){
            self.getItem().appendTo(self.frame);
        });
        return this;
    },
    setForm: function(form){
        this.form = form;
    },
    getItem: function(){
        this.itemCount += 1;
        var self = this;
        var item = $([
            '<div class="item">',
            '    <div class="input-group" style="width:100%">',
            '        <input type="text" class="form-control begin-time" placeholder="开始时间">',
            '        <span class="input-group-addon" style="border-left: none;border-right: none;">-</span>',
            '        <input type="text" class="form-control end-time" placeholder="结束时间">',
            '        <span class="input-group-addon" style="border-left: none;border-right: none;">></span>',
            '        <input type="text" class="form-control unit" placeholder="计费单位">',
            '        <span class="input-group-addon" style="border-left: none;border-right: none;">:</span>',
            '        <input type="text" class="form-control price" placeholder="单位价格">',
            '        <span class="input-group-btn">',
            '            <button class="btn btn-default btn-sm btn-drop" type="button" style="border-radius: 0;"><i class="fa fa-times"></i></button>',
            '        </span>',
            '    </div>',
            '    <span class="form-static" style="display: none;"></span>',
            '</div>'
        ].join(''));
        item.find('.begin-time').css({float: 'left', width: '90px', height: '30px'});
        item.find('.input-group-addon').css({float: 'left', width: '30px', height: '30px'});
        item.find('.end-time').css({float: 'left', width: '90px', height: '30px'});
        item.find('.unit').css({float: 'left', width: '90px', height: '30px'});
        item.find('.price').css({float: 'left', width: 'calc(100% - 390px)', height: '30px'});
        item.find('.input-group-btn').css({float: 'left'});
        item.find('.btn-drop').click(function(){
            item.remove();
        });
        return item;
    },
    change: function(val){//改变
        this.reset();
        val = JSON.parse(val) ? JSON.parse(val) : '';
        for(var i in val){
            var item = val[i];
            var price = item['price'] ? item['price'] : '';
            var beginTime = item['beginTime'] ?item['beginTime'] : '';
            var endTime = item['endTime'] ? item['endTime'] : '';
            var unit = item['unit'] ? item['unit'] : '';
            if(i+'' === '0'){
                this.frame.find('.item.main .price').val(price);
                this.frame.find('.item.main .begin-time').val(beginTime);
                this.frame.find('.item.main .end-time').val(endTime);
                this.frame.find('.item.main .unit').val(unit);
            }else{
                var itemObj = this.getItem();
                itemObj.find('.price').val(price);
                itemObj.find('.begin-time').val(beginTime);
                itemObj.find('.end-time').val(endTime);
                itemObj.find('.unit').val(unit);
                itemObj.appendTo(this.frame);
            }
        }
        return this;
    },
    view: function(){//查看
        this.flushView();
        this.frame.find('.item > .input-group').hide();
        this.frame.find('.item > .form-static').show();
        return this;
    },
    flushView: function(){
        this.frame.find('.item').each(function(){
            var text = '从第 <span style="color: blue;">'+$(this).find('.begin-time').val() + '</span> 分到第 <span style="color: blue;">' + $(this).find('.end-time').val()+'</span> 分，每 <span style="color: blue;">'+$(this).find('.unit').val()+'</span> 分 <span style="color: red;">';
            text += $(this).find('.price').val()+'</span> 元';
            $(this).find('.form-static').html('<span style="font-size: 13px;">'+text+'</span>');
        });
    },
    reset: function(){
        this.frame.find('.item').each(function(){
            if(!$(this).hasClass('main')){
                $(this).remove();
            }
        });
        this.frame.find('.item.main').find('select').val('').change();
        this.frame.find('.item.main').find('input').val('').change();
        this.frame.find('.item > .input-group').show();
        this.frame.find('.item > .form-static').hide();
        return this;
    },
    edit: function(){
        this.frame.find('.item > .input-group').show();
        this.frame.find('.item > .form-static').hide();
        return this;
    },
    value: function(){
        var self = this;
        var data = [];
        this.frame.find('.item').each(function(){
            var price = $(this).find('.price').val();
            var beginTime = $(this).find('.begin-time').val();
            var endTime = $(this).find('.end-time').val();
            var unit = $(this).find('.unit').val();
            data.push({
                price: price,
                beginTime: beginTime,
                endTime: endTime,
                unit: unit
            });
        });
        return JSON.stringify(data);
    }, 
    enable: function(){
        this.frame.find('select').prop('disabled', false);
        this.frame.find('input').prop('disabled', false);
        return this;
    },
    disable: function(){
        this.frame.find('select').prop('disabled', true);
        this.frame.find('input').prop('disabled', true);
        return this;
    },
    validate: function(){
        var rs = true;
//        this.frame.find('.input-group').each(function(){
//            var epgId = $(this).find('select').val();
//            var times = $(this).find('input').val();
//            if((epgId.length < 1) && (times.length > 1)){
//                $(this).find('select').addClass('error').one('change', function(){
//                    $(this).removeClass('error');
//                });
//                rs = false;
//            }else if((epgId.length > 1) && (times.length < 1)){
//                $(this).find('input').addClass('error').one('change', function(){
//                    $(this).removeClass('error');
//                });
//                rs = false;
//            }
//        });
        return rs; 
    },
    onChange: function(){
        if(this.config.onChange){
            this.config.onChange(this);
        }
    },
    destroy: function(){
        this.frame.remove();
    }
};
//添加到库
App.Form.lib['admin-finance-price-time'] = Admin.Finance.Price.Form.Timeprice;